﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SerialPortExamples
{
    public class Protocol
    {
        /// <summary>
        /// START
        /// </summary>
        byte[] Start = { 0x53, 0x54, 0x41, 0x52, 0x54 };
        /// <summary>
        /// Version，默认为0
        /// </summary>
        byte[] Version = { 0x00 };
        /// <summary>
        /// 内容标识
        /// </summary>
        public byte Flag;
        /// <summary>
        /// 传输方向，上位机=>FPGA 0XAA
        /// </summary>
        byte[] Direction = { 0xAA };
        /// <summary>
        /// 信息预留
        /// </summary>
        byte[] Preserve = { 0x00,0x00,0x00,0x00 };
        /// <summary>
        /// 起始地址位
        /// </summary>
        public byte[] StartIndex = { 0x15 };
        /// <summary>
        /// 16*8 byte表示数据长度
        /// </summary>
        public byte[] DataLength ;
        /// <summary>
        /// 数据
        /// </summary>
        public byte[] Data ;
        /// <summary>
        /// END
        /// </summary>
        public byte[] End = { 0x45, 0x4E, 0x44 };

        public byte[] ToByteArray()
        {
            List<byte> data = new List<byte>();
            data.AddRange(Start);
            data.AddRange(Version);
            data.Add(Flag);
            data.AddRange(Direction);
            data.AddRange(Preserve);
            data.AddRange(StartIndex);
            data.AddRange(DataLength);
            data.AddRange(Data);
            data.AddRange(End);
            return data.ToArray();
        }

        public Protocol(byte flag, byte[] data, byte[] dataLength)
        {
            this.Flag = flag;
            this.Data = data;
            this.DataLength = dataLength;
        }
    }
}
